rule genotype_gvcf:
    input:
        gvcf="{sample}.gvcf.gz",
        ref_fasta=config["ref_fasta"],
        dbsnp_vcf=config["dbsnp"],
    output:
        raw_vcf="{sample}.raw.vcf.gz",
        raw_vcf_tbi="{sample}.raw.vcf.gz.tbi",
    shell:
        """
        gatk GenotypeGVCFs \
            -V {input.gvcf} \
            -O {output.raw_vcf} \
            -R {input.ref_fasta} \
            --dbsnp {input.dbsnp_vcf} \
            -G StandardAnnotation \
            -G StandardHCAnnotation \
            --allow-old-rms-mapping-quality-annotation-data true \
            -new-qual true \
            -OVI true
        """

rule normalize_vcf:
    input:
        raw_vcf="{sample}.raw.vcf.gz",
        ref_fasta=config["ref_fasta"],
    output:
        vaf_vcf=temp("{sample}.vaf.vcf.gz"),
        vaf_vcf_tbi=temp("{sample}.vaf.vcf.gz.tbi"),
        norm_vcf="{sample}.norm.vcf.gz",
        norm_vcf_tbi="{sample}.norm.vcf.gz.tbi",
    shell:
        """
        bcftools +fill-tags {input.raw_vcf} -Oz -o {output.vaf_vcf} -- -t VAF && \
        bcftools index --tbi {output.vaf_vcf} && \
        bcftools norm -f {input.ref_fasta} -m -any {output.vaf_vcf} | \
        awk '$1~/#/ || $5!~/[^ATGC]/' | bgzip -c > {output.norm_vcf} && \
        bcftools index --tbi {output.norm_vcf}
        """


if config.get("hard_filtering"):
    include: "hard_filtering.smk"
else:
    include: "vqsr.smk"